package com.hanson.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.entity.ScoreSeqBean;
import com.hanson.entity.SpreadInfoBean;
import com.hanson.entity.UserBean;
import com.hanson.mapper.ScoreSeqBeanMapper;
import com.hanson.mapper.UserBeanMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.Date;
import java.util.List;

@Service
public class ScoreService {
    @Autowired
    private ScoreSeqBeanMapper scoreSeqBeanMapper;
    @Autowired
    private UserBeanMapper userBeanMapper;

    public List<ScoreSeqBean> getScoreInfoPageByOpenId(String openId, Integer page,Integer limit) {
        UserBean userBean = userBeanMapper.selectByOpenId(openId);
        List<ScoreSeqBean> scoreSeqBeanList = null;
        if (null != userBean) {
            Page<ScoreSeqBean> pageInfo = new Page<>(page, limit);
            scoreSeqBeanList = scoreSeqBeanMapper.selectPageByOpenId(pageInfo,openId);
        }
        return scoreSeqBeanList;
    }

    public List<ScoreSeqBean> getScoreInfoByOpenId(String openId, ModelMap modelMap) {
        UserBean userBean = userBeanMapper.selectByOpenId(openId);
        List<ScoreSeqBean> scoreSeqBeanList = null;
        if (null != userBean) {
            modelMap.put("totalScore", userBean.getScore());
            scoreSeqBeanList = scoreSeqBeanMapper.selectByOpenId(openId);
            if (CollectionUtils.isNotEmpty(scoreSeqBeanList)) {
                int totalGetScore = 0;
                int totalPutScore = 0;
                int todayScore = 0;
                for (ScoreSeqBean scoreSeqBean : scoreSeqBeanList) {
                    if (scoreSeqBean.getType() == 1) {
                        totalGetScore += scoreSeqBean.getScore();
                        if (DateUtils.isSameDay(new Date(), scoreSeqBean.getCreateDate())) {
                            todayScore += scoreSeqBean.getScore();
                        }
                    } else if (scoreSeqBean.getType() == 2) {
                        totalPutScore += scoreSeqBean.getScore();
                    }
                }
                modelMap.put("totalGetScore", totalGetScore);
                modelMap.put("totalPutScore", totalPutScore);
                modelMap.put("todayScore", todayScore);
            }
        }
        return scoreSeqBeanList;
    }
}
